package com.ytjj.qmyx.admin.service.impl;

import com.github.pagehelper.PageHelper;
import com.ytjj.common.api.CommonPage;
import com.ytjj.common.exception.ApiException;
import com.ytjj.qmyx.admin.dao.*;
import com.ytjj.qmyx.admin.model.request.ProductLibraryRequest;
import com.ytjj.qmyx.admin.model.response.ProductLibraryResponse;
import com.ytjj.qmyx.admin.service.ProductLibraryService;
import com.ytjj.qmyx.mall.mapper.*;
import com.ytjj.qmyx.mall.model.*;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;

@Service
public class ProductLibraryServiceImpl implements ProductLibraryService {
    @Resource
    private ProductCategoryMapper productCategoryMapper;
    @Resource
    private BankerMapper bankerMapper;
    @Resource
    private ProductLibraryDao productLibraryDao;
    @Resource
    private ProductSelectMapper productSelectMapper;
    @Resource
    private ProductLibrarySkuKeyDao productLibrarySkuKeyDao;
    @Resource
    private ProductSelectSkuKeyDao productSelectSkuKeyDao;
    @Resource
    private ProductSelectSkuKeyMapper selectSkuKeyMapper;
    @Resource
    private ProductLibrarySkuValueMapper productLibrarySkuValueMapper;
    @Resource
    private ProductSelectSkuValueDao productSelectSkuValueDao;
    @Resource
    private ProductSelectSkuValueMapper selectSkuValueMapper;
    @Resource
    private ProductLibrarySpecMapper productLibrarySpecMapper;
    @Resource
    private ProductSelectSpecMapper selectSpecMapper;


    @Override
    public CommonPage<ProductLibraryResponse> selectList(ProductLibraryRequest productLibraryRequest) {
        PageHelper.startPage(productLibraryRequest.getPageNum(), productLibraryRequest.getPageSize());
        List<ProductLibraryResponse> page = productLibraryDao.selectList(productLibraryRequest);
        if (!CollectionUtils.isEmpty(page)) {
            for (ProductLibraryResponse productLibraryResponse : page) {
                //根据categoryId拿到的是二级分类信息
                ProductCategory productCategory = productCategoryMapper.selectByPrimaryKey(productLibraryResponse.getCategroyPid());
                if (productCategory != null) {
                    productLibraryResponse.setCategoryTitle(productCategory.getTitle());
                }
                //获取一级分类信息
                if (productCategory != null && productCategory.getPid() != 0) {
                    ProductCategory pcTop = productCategoryMapper.selectByPrimaryKey(productLibraryResponse.getCategoryId());
                    productLibraryResponse.setCategoryTopTitle(pcTop.getTitle());
                } else if (productCategory != null && productCategory.getPid() == 0) {
                    productLibraryResponse.setCategoryTopTitle(productCategory.getTitle());
                }
                //获取bankerName
                Banker banker = bankerMapper.selectByPrimaryKey(productLibraryResponse.getBankerId());
                if (banker != null) {
                    productLibraryResponse.setBankerName(banker.getName());
                }
            }
        }
        return CommonPage.restPage(page);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertProductSelect(ProductLibrary productLibrary) {
        Banker banker = bankerMapper.selectByPrimaryKey(productLibrary.getBankerId());
        if (banker == null){
            throw new ApiException("该店家id{" + productLibrary.getBankerId() + "}不存在！");
        }
        int count = productLibraryDao.updateIsSelect(productLibrary);
        if (count > 0) {

            ProductLibrary productLibrary1 = productLibraryDao.selectById(productLibrary.getId());
            ProductSelect productSelect = new ProductSelect();
            productSelect.setProductLibraryId(productLibrary1.getId());
            productSelect.setProductNo(productLibrary1.getProductNo());
            productSelect.setProductName(productLibrary1.getProductName());
            productSelect.setProductImg(productLibrary1.getProductImg());
            productSelect.setDescr(productLibrary1.getDescr());
            productSelect.setSort(productLibrary1.getSort());
            productSelect.setCostPrice(productLibrary1.getCostPrice());
            productSelect.setStock(productLibrary1.getStock());
            productSelect.setWarehouse(productLibrary1.getWarehouse());
            productSelect.setWarehouseName(productLibrary1.getBankerName());
            productSelect.setCategoryId(productLibrary1.getCategroyPid());
            productSelect.setAttributeJson(productLibrary1.getAttributeJson());
            productSelect.setCreateTime(productLibrary1.getCreateTime());
            productSelect.setUpdateTime(productLibrary1.getUpdateTime());
            productSelect.setBankerId(productLibrary1.getBankerId());
            productSelect.setRate(productLibrary1.getRate());
            productSelect.setReceStatus((byte) 1);
            productSelect.setCompleteStatus((byte) 0);
            productSelect.setSelectStatus(0);
            count = productSelectMapper.insertSelective(productSelect);
            //添加SkuKey
            List<ProductLibrarySkuKey> librarySkuKeys = productLibrarySkuKeyDao.selectByPid(productLibrary.getId());
            for (ProductLibrarySkuKey librarySkuKey : librarySkuKeys) {
                ProductSelectSkuKey productSelectSkuKey = new ProductSelectSkuKey();
                productSelectSkuKey.setPid(productLibrary.getId());
                productSelectSkuKey.setName(librarySkuKey.getName());
                productSelectSkuKey.setSort(librarySkuKey.getSort());
                productSelectSkuKey.setCreateTime(librarySkuKey.getCreateTime());
                productSelectSkuKey.setUpdateTime(librarySkuKey.getUpdateTime());
                productSelectSkuKey.setAttributeKeyId(librarySkuKey.getKid());
                ProductSelectSkuKey selectSkuKey = productSelectSkuKeyDao.selectByPidAndAttributeKeyId(librarySkuKey.getPid(),librarySkuKey.getKid());
                if (selectSkuKey == null) {
                    selectSkuKeyMapper.insertSelective(productSelectSkuKey);
                }
            }
            //添加SkuValue
            ProductLibrarySkuValueExample productLibrarySkuValueExample = new ProductLibrarySkuValueExample();
            productLibrarySkuValueExample.createCriteria().andPidEqualTo(productLibrary.getId());
            List<ProductLibrarySkuValue> librarySkuValues = productLibrarySkuValueMapper.selectByExample(productLibrarySkuValueExample);
            for (ProductLibrarySkuValue librarySkuValue : librarySkuValues) {
                ProductSelectSkuValue productSelectSkuValue = new ProductSelectSkuValue();
                productSelectSkuValue.setPid(librarySkuValue.getPid());
                productSelectSkuValue.setSort(librarySkuValue.getSort());
                productSelectSkuValue.setStatus(librarySkuValue.getStatus());
                productSelectSkuValue.setValue(librarySkuValue.getValue());
                productSelectSkuValue.setCreateTime(librarySkuValue.getCreateTime());
                productSelectSkuValue.setUpdateTime(librarySkuValue.getUpdateTime());
                productSelectSkuValue.setAttributeValueId(librarySkuValue.getVid());
                ProductSelectSkuValue productSelectSkuValue1 = productSelectSkuValueDao.selectByPidAndAttributeValueId(librarySkuValue.getPid(), librarySkuValue.getVid());
                if (productSelectSkuValue1 == null) {
                    selectSkuValueMapper.insertSelective(productSelectSkuValue);
                }
            }
            //添加规格
            ProductLibrarySpecExample productLibrarySpecExample = new ProductLibrarySpecExample();
            productLibrarySpecExample.createCriteria().andProductIdEqualTo(productLibrary.getId());
            List<ProductLibrarySpec> productLibrarySpecs = productLibrarySpecMapper.selectByExample(productLibrarySpecExample);
            for (ProductLibrarySpec productLibrarySpec : productLibrarySpecs) {
                ProductSelectSpec productSelectSpec = new ProductSelectSpec();
                productSelectSpec.setPrice(productLibrarySpec.getPrice());
                productSelectSpec.setProductLibraryId(productLibrarySpec.getProductId());
                productSelectSpec.setProImg(productLibrarySpec.getProImg());
                productSelectSpec.setSkuSpec(productLibrarySpec.getSkuSpec());
                productSelectSpec.setStatus(productLibrarySpec.getStatus());
                productSelectSpec.setStock(productLibrarySpec.getStock());
                productSelectSpec.setCreateTime(productLibrarySpec.getCreateTime());
                productSelectSpec.setUpdateTime(productLibrarySpec.getUpdateTime());
                selectSpecMapper.insertSelective(productSelectSpec);
            }
        }
        return count;
    }
}
